/**
 * Easy ui dataGrid	功能扩展
 * Easy ui treeGrid 功能扩展
 */
$(function(){
	$.extend($.fn.datagrid.methods,{
		getRowById:function(target,idValue){
			target=target[0];
			var opts = $.data(target, 'datagrid').options;
			var data = $.data(target, 'datagrid').data;
			
			if (opts.idField){
				var index = -1;
				for(var i=0; i<data.rows.length; i++){
					if (data.rows[i][opts.idField] == idValue){
						index = i;
						break;
					}
				}
				if (index >= 0){
					return data.rows[index];
				}else{
					return null;
				}
			}
		},
		deleteRowById:function(target,idValue){
			var index=target.datagrid("getRowIndex",idValue);
			if(index>=0){
				target.datagrid('deleteRow',index);
			}
		}
	});

	/**
	 * 扩展dataGrid的编辑器
	 * 增加以下功能：
	 * userSelectEdit:用户选择功能
	 */
	$.extend($.fn.datagrid.defaults.editors, {
		userSelectEditor:{
			init:function(container, options){
				options=options?options:{};
				var input=$("<input>",{
				}).appendTo(container);
				input.click(function(){
					options.onSelect=function(users){
						input.data("users",users);
						container.data("users",users);
						var u=[];
						$.each(users,function(i,n){
							u.push(this.name);
						});
						input.val(u.join(","));
					};
					options.users=container.data("users");
					winflag.utils.selectUsers(options);
				});
				return input;
			},
			getValue: function(target){  
		        return $(target).data("users");  
		    },
		    setValue:function(target, value){
		    	return $(target).val(value);
		    },
		    resize: function(target, width) {
		    	var span = $(target);
		    	if ($.boxModel == true){  
		    		span.width(width - (span.outerWidth() - span.width()) - 10);  
		    	}else {  
		            span.width(width - 10);  
		        }  
		    }
		},
	iconSelectEditor:{
		init:function(container, options){
			options=options?options:{};
			var input=$("<input>",{
			}).appendTo(container);
			return input;
		},
		getValue: function(target){
			return $(target).val();
		},
		setValue:function(target, value){
			return $(target).val(value);
		},
		resize: function(target, width) {
			var span = $(target);
	    	if ($.boxModel == true){  
	    		span.width(width - (span.outerWidth() - span.width()) - 10);  
	    	}else {  
	            span.width(width - 10);  
	        } 
		}
	},
	funPointSelectEditor:{
		init:function(container, options){
			options=options?options:{};
			var input=$("<input>",{
			}).appendTo(container);
			
			input.click(function(){
				options.onSelect=function(funPoints){
					if(funPoints && funPoints.length>0){
						options.funPoint=funPoints[0];
						input.val(options.funPoint.funPointName);
						input.data("funpoint",options.funPoint);
					}else{
						options.funPoint=null;
						input.val("");
						input.data("funpoint",null);
					}
				};
				winflag.utils.selectFunPoint(options);
			});
			
			return input;
		},
		getValue: function(target){
			return $(target).data("funpoint");
		},
		setValue:function(target, value){
			$(target).val(value);
		},
		resize: function(target, width) {
			var span = $(target);
	    	if ($.boxModel == true){  
	    		span.width(width - (span.outerWidth() - span.width()) - 10);  
	    	}else {  
	            span.width(width - 10);  
	        } 
		}
	}
	});
});
